package com.gsp.gspframework.helper;

import com.gsp.gspframework.annotation.Controller;
import com.gsp.gspframework.annotation.Service;
import com.gsp.gspframework.util.ClassUtil;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public final class ClassHelper {

    private static final Set<Class<?>> CLASS_SET;

    static {
        CLASS_SET = ClassUtil.getClassSet(ConfigHelper.getAppBasePackage());
    }

    public static Set<Class<?>> getClassSet() {
        return CLASS_SET;
    }

    public static Set<Class<?>> getControllerClassSet() {
        return getClassSetByAnnotation(Controller.class);
    }

    public static Set<Class<?>> getServiceClassSet() {
        return getClassSetByAnnotation(Service.class);
    }

    public static Set<Class<?>> getBeanClassSet() {
        Set<Class<?>> clzSet = new HashSet<>();
        clzSet.addAll(getServiceClassSet());
        clzSet.addAll(getControllerClassSet());
        return clzSet;
    }

    /**
     * 获取父类(接口)的所有子类(实现)
     */
    public static Set<Class<?>> getClassSetBySuper(Class<?> superClass) {
        return CLASS_SET.stream().filter(clz -> superClass.isAssignableFrom(clz) && !superClass.equals(clz)).collect(Collectors.toSet());
    }

    /**
     * 获取带某注解的所有类
     */
    public static Set<Class<?>> getClassSetByAnnotation(Class<? extends Annotation> superClass) {
        return CLASS_SET.stream().filter(clz -> clz.isAnnotationPresent(superClass)).collect(Collectors.toSet());
    }

}
